package coma.dou.dema;

import com.solidfire.gson.Gson;


import java.util.*;

public class demo2 {
    public static void main(String[] args) {
        List<Good> goods = new ArrayList<>();
        Good good1 = new Good(1L,"100000","江苏","101000","南京", "101010", "江陵");
        goods.add(good1);
        Good good2 = new Good(2L,"100000","江苏","101000","南京", "101011", "六合");
        goods.add(good2);
        Good good3 = new Good(3L,"100000","江苏","102000","苏州", "102010", "江阴");
        goods.add(good3);
        Good good4 = new Good(4L,"100000","江苏","102000","苏州", "102011", "昆山");
        goods.add(good4);
        Good good5 = new Good(5L,"200000","浙江","201000","杭州", "201010", "上城区");
        goods.add(good5);


        Set<Tree> good1s = new HashSet<>();
        Set<Tree> good2s = new HashSet<>();
        Set<Tree> good3s = new HashSet<>();

        for (Good good : goods) {
            good1s.add(new Tree(1, good.getCode1(), "", good.getName1(),null));
            good2s.add(new Tree(2, good.getCode2(), good.getCode1(), good.getName2(),null));
            good3s.add(new Tree(3,good.getCode3(), good.getCode2(), good.getName3(),null));
        }

        System.out.println(good1s);
        for (Tree tree : good1s) {
            String code1 = tree.getCode();
            tree.setTrees(good1s);

            Set<Tree>  g2 =  getTree2s(good2s, code1);
            tree.setTrees(g2);

            for (Tree tree2 : g2) {
                String code2 = tree2.getCode();
                Set<Tree>  g3 =  getTree2s(good3s, code2);
                tree2.setTrees(g3);
            }

        }

        System.out.println(good1s);
        System.out.println("===================================");
        String json = new Gson().toJson(good1s);

        System.out.println(json);
    }

    // 2
    private static Set<Tree> getTree2s(Set<Tree> trees, String code){
        Set<Tree> treeList =  new HashSet<>();
        for (Tree tree : trees) {
            if(code.equals(tree.getPcode())){
                treeList.add(tree);
            }
        }
        return treeList;
    }


}
